/*
 *      RCSAgent.java
 *      
 *      Copyright 2010 Mark Kelly <markkelly0808@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package org.RCSAgent;

import java.util.regex.Pattern;

import org.RCSAgent.communication.networking.NetworkCommunicator;
import org.RCSAgent.gameelements.BodySense;
import org.RCSAgent.gameelements.Score;
import org.RCSAgent.gameelements.Side;

public abstract class RCSAgent {
	public static final int VERSION = 13;
	
	private NetworkCommunicator netCom;
	private RCSSServerParser parser;
	
	private String team;
	private int version;
	private Side side;
	
	public RCSAgent(String team, int version, String hostName, int port) {
		this.team = team;
		this.version = version;
		
		this.netCom = new NetworkCommunicator(hostName, port);
		this.parser = new RCSSServerParser();
	}
	
	public RCSAgent(String team, int version, int port) {
		this(team, version, NetworkCommunicator.DEFAULT_HOST_NAME, port);
	}
	
	public RCSAgent(String team, int version, String hostName) {
		this(team, version, hostName, NetworkCommunicator.DEFAULT_PORT);
	}
	
	public RCSAgent(String team, int version) {
		this(team, version, NetworkCommunicator.DEFAULT_PORT);
	}
	
	public RCSAgent(String team) {
		this(team, RCSAgent.VERSION);
	}
	
	private void init() {
		
	}
	
	public void attentionTo(String side, int number) {		
		netCom.send("(attentionto " + side + " " + number + ")");
	}
	
	public void attentionToOff() {
		netCom.send("(attentionto off)");
	}

	public void catchBall(float direction) {
		netCom.send("(catch " + direction +")");
	}

	public void changeView(String width, String quality) {
		netCom.send("(change_view " + width + " " + quality + ")");
	}

	public void dash(float power, float direction) {
		netCom.send("(dash " + power + " " + power + ")");
	}

	public void kick(float power, float direction) {
		netCom.send("(kick " + power + " " + direction +")");
	}

	public void move(float x, float y) {
		netCom.send("(move " + x + " " + y + ")");
	}

	public void pointTo(float distance, float direction) {
		netCom.send("(pointto " + distance + " " + direction + ")");
	}
	
	public void pointToOff() {
		netCom.send("(pointto off)");
	}

	public void say(String message) {
		netCom.send("(say \"" + message + "\")");
	}

	public BodySense senseBody() {
		netCom.send("(sense_body)");
		
		// TODO parse sense_body message
		return new BodySense();
	}

	public Score score() {
		netCom.send("(score)");
		
		// TODO parse score message
		return new Score();
	}

	public void tackle(float direction) {
		netCom.send("(tackle " + direction + ")");
	}

	public void turn(float moment) {
		netCom.send("(turn " + moment + ")");
	}
	
	public void turnNeck(float angle) {
		netCom.send("(turn_neck " + angle + ")");
	}
	
	private class RCSSServerParser {
		private final Pattern initPattern = Pattern.compile("\\(init\\s+([lr])\\s+(\\d{1,2})\\s+(\\w+)\\)");
		private final Pattern paramGroupPattern = Pattern.compile("\\(server_param\\s+(\\((\\w+)\\s+(.*)\\)\\s*)*\\)");
		private final Pattern paramPattern = Pattern.compile("\\((\\w+)\\s+(.*)\\)");
		
		public RCSSServerParser() {
		}
	}
}
